<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var permutation = function (S) {
      var temp = S.split(''); //字符串转为数组
      len = temp.length;//长度
      var res = []; //结果
      dfs(0);
      return res;
      function dfs(k) {
        if (k == len) { //结束条件
          res.push(temp.join('')); //加入结果
          return;
        }
        for (let i = k; i < len; i++) {
          [temp[i], temp[k]] = [temp[k], temp[i]]; //交换位置
          dfs(k + 1);                              //递归
          [temp[i], temp[k]] = [temp[k], temp[i]]; //回溯
        }
      }
    };
    console.log(permutation(S = "qwe"))
  </script>
</body>

</html>